package com.plian.system.service.fr.audit.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.Tools.StringUtil;
import com.plian.system.dto.fr.audit.AuditReportDTO;
import com.plian.system.entity.fr.audit.AuditSystem;
import com.plian.system.mapper.fr.audit.AuditSystemMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.fr.audit.IAuditSystemService;
import com.plian.system.vo.fr.audit.AuditSystemStatisticsVO;
import com.plian.system.vo.report.HeaderVO;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.vo.report.ReportVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 审计制度 服务实现类
 *
 * @author gangan
 * @since 2019-09-27
 */
@Slf4j
@Service
@AllArgsConstructor
public class AuditSystemServiceImpl extends BaseServiceImpl<AuditSystemMapper, AuditSystem> implements IAuditSystemService {

    @Override
    public Boolean deleteLogicByReportId(List<Long> ids) {
        LambdaQueryWrapper<AuditSystem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AuditSystem::getAuditReportId, ids);
        List<AuditSystem> auditProjects = list(queryWrapper);
        if (!CollectionUtils.isEmpty(auditProjects)){
            return deleteLogic(auditProjects.stream().map(AuditSystem::getId).collect(Collectors.toList()));
        }
        return true;
    }

    @Override
    public ReportReturnVO auditSystemStatistics(AuditReportDTO auditReportDTO) {
        if (StringUtil.isBlank(auditReportDTO.getFillingYear())){
            auditReportDTO.setFillingYear(DateUtil.format(DateUtil.now(), DateUtil.PATTERN_YEAR));
        }
        List<AuditSystemStatisticsVO> auditContactStatisticsVOS = baseMapper.auditSystemStatistics(auditReportDTO);
        if (!CollectionUtils.isEmpty(auditContactStatisticsVOS)){
            for (AuditSystemStatisticsVO statisticsVO : auditContactStatisticsVOS){
               List<AuditSystem> auditSystems = statisticsVO.getAuditSystems();
               if (CollectionUtil.isNotEmpty(auditSystems)){
                   statisticsVO.setNumber(auditSystems.size());
                   statisticsVO.setAuditSystemNames(auditSystems.stream().map(AuditSystem::getAuditSystemName).collect(Collectors.joining(",")));
               }
            }
        }

        ReportReturnVO reportReturnVO = new ReportReturnVO();
        ReportVO reportVO = new ReportVO();

        reportVO
                .addHeader(HeaderVO.builder().key("companyName").title("企业名称").build())
                .addHeader(HeaderVO.builder().key("number").title("数量").build())
                .addHeader(HeaderVO.builder().key("auditSystemNames").title("内部审计相关制度名称").build());
        reportVO.setData(auditContactStatisticsVOS);
        reportReturnVO.setGrids(reportVO);
        return reportReturnVO;
    }

    @Override
    public List<AuditSystemStatisticsVO> getExportAuditSystemStatistics(AuditReportDTO auditReportDTO) {
        if (StringUtil.isBlank(auditReportDTO.getFillingYear())){
            auditReportDTO.setFillingYear(DateUtil.format(DateUtil.now(), DateUtil.PATTERN_YEAR));
        }
        List<AuditSystemStatisticsVO> auditContactStatisticsVOS = baseMapper.auditSystemStatistics(auditReportDTO);
        if (!CollectionUtils.isEmpty(auditContactStatisticsVOS)){
            for (AuditSystemStatisticsVO statisticsVO : auditContactStatisticsVOS){
                List<AuditSystem> auditSystems = statisticsVO.getAuditSystems();
                if (CollectionUtil.isNotEmpty(auditSystems)){
                    statisticsVO.setNumber(auditSystems.size());
                    statisticsVO.setAuditSystemNames(auditSystems.stream().map(AuditSystem::getAuditSystemName).collect(Collectors.joining( String.valueOf((char)10))));
                }
            }
        }
        return auditContactStatisticsVOS;
    }

}
